<?php
/**
 * 正常报文解析测试
 * 测试厂家修复后的正常MQTT报文
 */

echo "========================================\n";
echo "正常报文解析测试\n";
echo "========================================\n\n";

// 真实的MQTT主题
$topic = 'rziot/zftx_w06/869778076234039/result';

// 正常的消息数据（厂家修复后）
$messageData = [
    'c' => 'zidianmao',
    'imei' => '869778076234039',
    'iccid' => '89860324462007170717',
    'ssid' => '4G-MIFI-152F',
    'password' => '1234567890',
    'opname' => '46003',
    'dbm' => '-77',  // ✅ 正常的dBm值
    'simSwitch' => '1',
    'ip' => '192.168.100.1',
    'lac' => '30555',
    'version' => 'JZ12SC_ZIDIANMAO_20251200_V1.0.0',
    'cell_id' => '125194008',
    'speed_downlink' => 0,
    'speed_uplink' => 0,
    'sn' => '86977807623403',
    'boot_time' => '11169',
    'count' => '8',
    'hidden' => '0'
];

echo "📡 MQTT主题: {$topic}\n";
echo "📊 消息数据:\n";
echo json_encode($messageData, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . "\n\n";

// ========== 测试1: 主题识别 ==========
echo "🔍 测试1: 主题识别\n";
echo "----------------------------------------\n";

$isVendor2Topic = strpos($topic, 'rziot/zftx_w06/') === 0 &&
                 strpos($topic, '/result') !== false;

if ($isVendor2Topic) {
    echo "✅ 主题识别成功: 这是Vendor2协议的主题\n\n";
} else {
    echo "❌ 主题识别失败\n";
    exit(1);
}

// ========== 测试2: IMEI提取 ==========
echo "🔍 测试2: IMEI提取\n";
echo "----------------------------------------\n";

$pattern = '/rziot\/zftx_w06\/([^\/]+)\/result/';
if (preg_match($pattern, $topic, $matches)) {
    $imei = $matches[1];
    echo "✅ IMEI提取成功: {$imei}\n\n";
} else {
    echo "❌ IMEI提取失败\n";
    exit(1);
}

// ========== 测试3: dBm值处理 ==========
echo "🔍 测试3: dBm值处理\n";
echo "----------------------------------------\n";

$dbm = $messageData['dbm'] ?? 0;
$dbmInt = intval($dbm);

echo "原始dBm值: {$dbm}\n";
echo "转换后dBm值: {$dbmInt}\n";

// 信号强度计算
$signalStrength = 3; // 默认值
if ($dbmInt >= -50) {
    $signalStrength = 5; // 极好
} elseif ($dbmInt >= -70) {
    $signalStrength = 4; // 好
} elseif ($dbmInt >= -85) {
    $signalStrength = 3; // 中等
} elseif ($dbmInt >= -100) {
    $signalStrength = 2; // 弱
} else {
    $signalStrength = 1; // 极弱
}

// 检查异常值
if ($dbmInt < -120 || $dbmInt > 0) {
    echo "⚠️  异常的dBm值，使用默认信号强度: 3\n";
    $signalStrength = 3;
} else {
    echo "✅ dBm值正常，计算信号强度\n";
}

echo "✅ 信号强度: {$signalStrength} (1-5)\n";
echo "   说明: -77dBm 在 -85 ~ -70 范围内，信号强度为3 (中等)\n\n";

// ========== 测试4: 运营商映射 ==========
echo "🔍 测试4: 运营商映射\n";
echo "----------------------------------------\n";

$opname = $messageData['opname'] ?? '';
$opcodeMap = [
    '46001' => '移动',
    '46002' => '电信',
    '46003' => '联通',
];
$mnc = $opcodeMap[$opname] ?? '未知';

echo "运营商代码: {$opname}\n";
echo "✅ 运营商: {$mnc}\n\n";

// ========== 测试5: 完整数据构建 ==========
echo "🔍 测试5: 完整数据构建\n";
echo "----------------------------------------\n";

$saveData = [
    'IMEI' => $imei,
    'mac' => $imei,
    'iccid' => $messageData['iccid'] ?? '',
    'signal_strength' => $signalStrength,
    'mnc' => $mnc,
    'ssid' => $messageData['ssid'] ?? '',
    'key' => $messageData['password'] ?? '',
    'main_sim' => $messageData['simSwitch'] ?? 0,
    'user_sim' => $messageData['simSwitch'] ?? 0,
    'hidden_ssid' => $messageData['hidden'] ?? 0,
    'conn_cnt' => $messageData['count'] ?? 0,
    'remain_pwr' => 100,
    'dbm' => $dbmInt,
    'update_time' => date('Y-m-d H:i:s'),
    'cell_id' => $messageData['cell_id'] ?? '',
    'lac' => $messageData['lac'] ?? '',
    'ip' => $messageData['ip'] ?? '',
    'version' => $messageData['version'] ?? '',
];

echo "✅ 数据构建成功\n\n";

// ========== 显示最终结果 ==========
echo "📋 最终解析结果:\n";
echo "========================================\n";
foreach ($saveData as $key => $value) {
    echo "{$key}: {$value}\n";
}
echo "========================================\n\n";

// ========== 验证关键字段 ==========
echo "🔐 验证关键字段:\n";
echo "----------------------------------------\n";

$checks = [
    'IMEI' => ['expected' => '869778076234039', 'actual' => $saveData['IMEI']],
    'iccid' => ['expected' => '89860324462007170717', 'actual' => $saveData['iccid']],
    'ssid' => ['expected' => '4G-MIFI-152F', 'actual' => $saveData['ssid']],
    'mnc' => ['expected' => '联通', 'actual' => $saveData['mnc']],
    'signal_strength' => ['expected' => 3, 'actual' => $saveData['signal_strength']],
    'dbm' => ['expected' => -77, 'actual' => $saveData['dbm']],
];

$allPassed = true;
foreach ($checks as $field => $check) {
    if ($check['expected'] == $check['actual']) {
        echo "✅ {$field}: {$check['actual']}\n";
    } else {
        echo "❌ {$field}: 期望 {$check['expected']}, 实际 {$check['actual']}\n";
        $allPassed = false;
    }
}

echo "\n";
if ($allPassed) {
    echo "🎉 所有验证通过！正常报文解析成功！\n";
    echo "\n📊 对比异常报文:\n";
    echo "   异常报文dBm: -18446744073709551532 → 信号强度: 3 (默认)\n";
    echo "   正常报文dBm: -77 → 信号强度: 3 (计算)\n";
    echo "   ✅ 两种情况都能正确处理！\n";
} else {
    echo "⚠️  部分验证失败\n";
    exit(1);
}

